package com.yeban.bams.client;

import com.yeban.bams.client.clientsocket.ClientRequest;
import com.yeban.bams.entity.Account;
import com.yeban.bams.server.serversocket.RequestTO;
import com.yeban.bams.service.Bank;

import javax.swing.*;
import java.awt.*;

/**
 * @Desc TODO
 * @Author yeban
 * @Date 2025/10/23
 */
public class ATMClient extends JFrame {

    // Panel
    MainPanel mainPanel;
    RegisterPanel registerPanel;
    LoginPanel loginPanel;
    BusinessPanel businessPanel;

    // 共同资源
    Account acc;

    public ATMClient() {
        // 设置窗体信息
        this.setTitle("BAMS Client");
        this.setSize(500, 650);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);

        // 实例化JPanel
        mainPanel = new MainPanel(this);
        registerPanel = new RegisterPanel(this);
        loginPanel = new LoginPanel(this);
        businessPanel = new BusinessPanel(this);

        // 设置窗体的布局方式为"卡片式布局"
        Container container = this.getContentPane();
        container.setLayout(new CardLayout());

        container.add("mainPanel", mainPanel);
        container.add("registerPanel", registerPanel);
        container.add("loginPanel", loginPanel);
        container.add("businessPanel", businessPanel);

        // 关闭窗口时显式登出当前账户
        this.addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
            public void windowClosing(java.awt.event.WindowEvent e) {
                if (acc != null) {
                    RequestTO reqLogout = new RequestTO(
                                    "logout",
                                    new Object[]{acc.getId()},
                                    new Class[]{Long.class}
                            );
                    ClientRequest.send(reqLogout);
                    acc = null;
                }
                // 停止可能存在的自动刷新
                if (businessPanel != null) {
                    businessPanel.stopAutoRefresh();
                }
            }
        });
    }

    public static void main(String[] args) {
        new ATMClient().setVisible(true);
    }

}
